package com.itheima.test05;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
服务端 接受上传的文件 并保存到服务器的磁盘 并给出响应
 */
public class ServerDemo {

    public static void main(String[] args) throws IOException {
        //创建服务器对象
        ServerSocket ss = new ServerSocket(8888);
        while (true) {
            //建立连接
            Socket accept = ss.accept();
            //获取输入流
            InputStream is = accept.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            //创建 磁盘输出流
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16\\copy\\a.jpg"));
            byte[] bytes = new byte[1024];
            int len;
            //边读边写
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            bos.flush();
            //向服务器响应 上传成功
            //获取 输出流
            OutputStream os = accept.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));

            //写数据
            writer.write("上传成功");
            //各种释放资源
            writer.close();
            bos.close();
        }
        //ss.close();

    }
}
